package com.android.tripspins.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.tripspins.app.MapActivity;
import com.android.tripspins.app.R;
import com.android.tripspins.app.bo.Trip;

/**
 * Created by joaortk on 01/08/13.
 */
public class TripListAdapter extends ArrayAdapter<Trip> {

    private final Context context;
    private final Trip[] trips;

    public TripListAdapter(Context context, Trip[] trips)
    {
        super(context, R.layout.trip_list_row, trips);
        this.trips = trips;
        this.context = context;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.trip_list_row, parent, false);

        TextView txtTitle = (TextView)rowView.findViewById(R.id.trip_row_title);
        txtTitle.setText(trips[position].getDescription());
        TextView txtPinCount = (TextView)rowView.findViewById(R.id.trip_row_pin_count);
        txtPinCount.setText(String.valueOf(trips[position].getPins().size()));

        TextView txtAttCount = (TextView)rowView.findViewById(R.id.trip_row_att_count);
        txtAttCount.setText(String.valueOf(trips[position].getCountAttachments()));

        ImageView imgMap = (ImageView)rowView.findViewById(R.id.trip_row_map);
        imgMap.setImageURI(trips[position].getRandomPicture());
        imgMap.setHorizontalFadingEdgeEnabled(true);

        final int pos = position;
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intentMapActivity = new Intent(context,MapActivity.class);
                intentMapActivity.putExtra("tripId", trips[pos].getId());
                context.startActivity(intentMapActivity);
            }
        });
        return rowView;
    }
}
